package com.zzy.blog.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class BlogContextListener implements ServletContextListener {

	private static WebApplicationContext context = null;

	public void contextDestroyed(ServletContextEvent event) {
	}

	public void contextInitialized(ServletContextEvent event) {
		loadBeansConfig(event);
	}

	public void loadBeansConfig(ServletContextEvent event) {
		context = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
	}

	public static Object getBeanByName(String beanName){
		try{
			return context.getBean(beanName);
		} catch (Exception e) {
			return null;
		}
	}

}
